package com.zygh.lz.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.HealthRecord;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.service.impl.HealthRecordService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api(tags = "健康档案")
@RestController
@RequestMapping("/HealthRecord")
public class HealthRecordController {
    @Resource
    private HealthRecordService healthRecordService;
    @Resource
    private StaffMapper staffMapper;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    @ApiOperation(value = "健康档案批量录入")
    @PostMapping("enteringRecord")
    public void enteringRecord(String path) throws IOException {
        getFile(path, 0);
    }


    @ApiOperation(value = "健康档案查询")
    @GetMapping("findRecord")
    public ResultBean enteringRecord(String card, String warningsignal,
                                     @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws IOException {
        PageHelper.startPage(pageNum, pageSize);
        List<HealthRecord> list = healthRecordService.list(new QueryWrapper<HealthRecord>()
                .eq(StringUtils.isNotEmpty(card), "card", card)
                .eq(StringUtils.isNotEmpty(warningsignal), "warningsignal", warningsignal));
        return ResultUtil.setOK("success", new PageInfo<>(list));
    }


    /**
     * @param path 图片文件完整路径
     * @param deep 深度，默认传0
     *             批量录入民警健康档案
     * @throws IOException 异常
     */
    private void getFile(String path, int deep) throws IOException {
        // 获得指定文件对象
        File file = new File(path);
        // 获得该文件夹内的所有文件
        File[] array = file.listFiles();

        for (int i = 0; i < array.length; i++) {
            //如果是文件
            if (array[i].isFile()) {
                //图片名称
                String name = array[i].getName();
                //去除后缀，取身份证号
                String ID = name.substring(0, name.indexOf("."));

                //生成新的图片路径
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String fileType = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();
                String tempFileName = System.currentTimeMillis() + uuid + "." + fileType;
                Date date = new Date();
                String format = simpleDateFormat.format(date.getTime());
                String picUrl = "D:/dlqw/picture/" + format ;


                // 指定要读取的图片
                FileInputStream in = new FileInputStream(new File(array[i].toString()));
                File file1 = new File(picUrl);
                if (!file1.exists()) file1.mkdirs();
                // 指定要写入的图片
                FileOutputStream out = new FileOutputStream(file1+ "/" + tempFileName);

                int n = 0;// 每次读取的字节长度
                byte[] bb = new byte[1024];// 存储每次读取的内容
                while ((n = in.read(bb)) != -1) {
                    out.write(bb, 0, n);// 将读取的内容，写入到输出流当中
                }
                // 关闭输入输出流
                out.close();
                in.close();

                Staff staffByID = staffMapper.findStaffByID(ID);
                HealthRecord healthRecord = new HealthRecord();
                healthRecord.setCard(ID);
                healthRecord.setRecord(format + "/" + tempFileName);
                if (staffByID != null) healthRecord.setWarningsignal(staffByID.getStaffNum());
                healthRecord.setCreatTime(new Date());
                healthRecordService.save(healthRecord);

            } else if (array[i].isDirectory()) {
                //如果是文件夹
                //System.out.println(array[i].getPath());
                //文件夹需要调用递归 ，深度+1
                getFile(array[i].getPath(), deep + 1);
            }
        }

    }


}
